﻿using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using Power.Common;
using Power.View;
using Power.View.Dialog;
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace Power.ViewModel
{
    public class MainViewModel : ViewModelBase
    {
        private static string imgSrc = "/Resources/images/";
        private MainWindow model;
        public MainViewModel(MainWindow model)
        {
            this.model = model;

            model.SizeChanged += Model_SizeChanged;
            this.model.Margin = new Thickness(0, 20, 20, 0);
            this.model.MaxHeight = SystemParameters.MaximizedPrimaryScreenHeight - 5;
            WindowCloseCommand = new RelayCommand(OnWindowCloseClick);
            WindowMinCommand = new RelayCommand(OnWindowMinClick);
            UserContent = GetSelectedUserControl(0);
            TabSwitch = new RelayCommand<object>(OnTabSwitch);
            TabSwitchWithTwo = new RelayCommand<Button>(OnTabSwitchWithTwo);
            MenuClick = new RelayCommand(OnMenuClick);
            Menu2Click = new RelayCommand(OnMenu2Click);
            WindowMaxCommand = new RelayCommand(OnWindowMaxCommand);
            ChangePasswordClick = new RelayCommand(OnChangePasswordClick);
            LoginClick = new RelayCommand(OnLoginClick);
            ProjectClick = new RelayCommand(OnProjectClick);
            AuthorityClick = new RelayCommand(OnAuthorityClick);
            SettingClick = new RelayCommand(OnSettingClick);
            ToMap = new RelayCommand(OnToMap);
            this.Data = new MainViewData() { ProName = "某某某管网AI", SystemName = "智能检测评估系统", UserName = "王小明", UserImage = "/Resources/images/user.png" };
        }

        private void OnTabSwitchWithTwo(Button obj)
        {
            //获取弹层所有按钮
            List<Button> btnList = Common.Common.GetChildObjects<Button>((StackPanel)obj.Parent, 0);
            btnList.ForEach((item) =>
            {
                //移除全部选中效果
                item.Foreground = new SolidColorBrush(Color.FromRgb(125, 125, 125));
            });

            //设置弹层按钮选中效果
            int index = Convert.ToInt32(btnList.IndexOf(obj));
            obj.Foreground = new SolidColorBrush(Color.FromRgb(0, 112, 195));
            //控制上箭头旋转成下箭头
            this.model.AIImage.RenderTransform = null;

            //处理抬头选中效果
            StackPanel sp = (StackPanel)this.model.AI.Parent;
            List<StackPanel> spList = Common.Common.GetChildObjects<StackPanel>((StackPanel)sp.Parent, 0);
            //移除所有同级元素选中效果
            for (int i = 0; i < spList.Count; i++)
            {
                var item = spList[i];
                List<Border> borderList = Common.Common.GetChildObjects<Border>(item);
                borderList[1].Height = 0;
                List<Label> labelList = Common.Common.GetChildObjects<Label>(item);
                labelList[0].Foreground = new SolidColorBrush(Color.FromRgb(125, 125, 125));
            }
            //设置当前元素的选中效果
            List<Border> CurBorderList = Common.Common.GetChildObjects<Border>(sp);
            CurBorderList[1].Height = 8;
            List<Label> CurLabelList = Common.Common.GetChildObjects<Label>(sp);
            CurLabelList[0].Foreground = new SolidColorBrush(Color.FromRgb(0, 112, 195));
            //设置跟换下拉图片(蓝色)
            List<Image> CurImageList = Common.Common.GetChildObjects<Image>(sp);
            CurImageList[0].Source = new BitmapImage(new Uri($"{imgSrc}bottomblue.png", UriKind.Relative));

            UserContent = GetSelectedUserControl(1, index);
            this.model.AIPopup.IsOpen = false;
        }

        private void Model_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            MaxImage = "";
        }

        private void OnToMap()
        {
            StaticCache.Map.MapHiddenShow(true);
            this.model.Close();   //关闭当前窗口
        }

        public string MaxImage
        {
            get
            {
                if (model.WindowState == WindowState.Maximized)
                {
                    return "/Resources/images/max_max.png";
                }
                else
                {
                    return "/Resources/images/max_normal.png";
                }
            }
            set
            {
                RaisePropertyChanged("MaxImage");
            }
        }
        /// <summary>
        /// 登录按钮点击
        /// </summary>
        private void OnLoginClick()
        {
            StaticCache.Map.MapHiddenShow();
            Login login = new Login();  //Login为窗口名，把要跳转的新窗口实例化
            login.WindowStartupLocation = WindowStartupLocation.CenterScreen;
            login.Show();  //打开新窗口
            this.model.Close();   //关闭当前窗口
        }
        /// <summary>
        /// 项目管理
        /// </summary>
        private void OnProjectClick()
        {
            ProjectManagement box = new ProjectManagement();
            var result = box.ShowDialog();
        }
        /// <summary>
        /// 权限管理
        /// </summary>
        private void OnAuthorityClick()
        {
            AuthorityManagement box = new AuthorityManagement();
            var result = box.ShowDialog();
        }
        /// <summary>
        /// 修改密码
        /// </summary>
        private void OnChangePasswordClick()
        {
            ChangePassword box = new ChangePassword();
            var result = box.ShowDialog();
        }
        /// <summary>
        /// 设置
        /// </summary>
        private void OnSettingClick()
        {
            AIDistinguishSettings box = new AIDistinguishSettings();
            box.ShowDialog();
        }

        /// <summary>
        /// 悬停菜单一
        /// </summary>
        private void OnMenuClick()
        {
            this.model.menu.IsOpen = true;
        }

        /// <summary>
        /// 悬停菜单二
        /// </summary>
        private void OnMenu2Click()
        {
            //显示悬停菜单
            this.model.menu1.IsOpen = true;
            //控制下箭头旋转成上箭头
            this.model.userImage2.RenderTransform = new RotateTransform(180, this.model.userImage2.Width / 2, this.model.userImage2.Height / 2);
        }

        //页面切换
        private void OnTabSwitch(object sender)
        {
            StackPanel sp = (StackPanel)((Button)sender).Parent;
            List<StackPanel> spList = Common.Common.GetChildObjects<StackPanel>((StackPanel)sp.Parent, 0);
            int index = Convert.ToInt32(spList.IndexOf(sp));
            if (index == 1)
            {
                this.model.AIPopup.IsOpen = true;
                this.model.AIImage.RenderTransform = new RotateTransform(180, this.model.userImage2.Width / 2, this.model.userImage2.Height / 2);
            }
            else
            {
                //移除所有同级元素选中效果
                for (int i = 0; i < spList.Count; i++)
                {
                    var item = spList[i];
                    List<Border> borderList = Common.Common.GetChildObjects<Border>(item);
                    borderList[1].Height = 0;
                    List<Label> labelList = Common.Common.GetChildObjects<Label>(item);
                    labelList[0].Foreground = new SolidColorBrush(Color.FromRgb(125, 125, 125));
                    if (i == 1) {  //ai识别特殊处理
                        List<Image> CurImageList = Common.Common.GetChildObjects<Image>(item);
                        CurImageList[0].Source = new BitmapImage(new Uri($"{imgSrc}bottom.png", UriKind.Relative));
                        //获取弹层所有按钮
                        List<Button> btnList = Common.Common.GetChildObjects<Button>(this.model.AIPopupSP,0);
                        btnList.ForEach((btnItem) =>
                        {
                            //移除全部选中效果
                            btnItem.Foreground = new SolidColorBrush(Color.FromRgb(125, 125, 125));
                        });
                    }
                }
                //设置当前元素的选中效果
                List<Border> CurBorderList = Common.Common.GetChildObjects<Border>(sp);
                CurBorderList[1].Height = 8;
                List<Label> CurLabelList = Common.Common.GetChildObjects<Label>(sp);
                CurLabelList[0].Foreground = new SolidColorBrush(Color.FromRgb(0, 112, 195));
                UserContent = GetSelectedUserControl(index);
            }
        }

        private UserControl GetSelectedUserControl(int index, int subIndex = 0)
        {
            UserControl userControl = null;
            switch (index)
            {
                case 0:
                    userControl = new ProjectOverview();
                    break;
                case 1:
                    if (subIndex == 0)
                        userControl = new AIDistinguish();
                    else if (subIndex == 1)
                        userControl = new AIReview();
                    else
                        userControl = null;
                    break;
                case 2:
                    userControl = new DataCenter();
                    break;
                case 3:
                    userControl = new AITool();
                    break;
                case 4:
                    userControl = new Setting();
                    break;
            }
            return userControl;
        }

        /// <summary>
        /// 窗口最小化
        /// </summary>
        private void OnWindowMinClick()
        {
            model.WindowState = WindowState.Minimized;
            MaxImage = "";
        }
        /// <summary>
        /// 关闭
        /// </summary>
        private void OnWindowCloseClick()
        {
            Application.Current.Shutdown();
        }
        /// <summary>
        /// 窗口最大化
        /// </summary>
        private void OnWindowMaxCommand()
        {
            model.WindowState = model.WindowState == WindowState.Maximized ? WindowState.Normal : WindowState.Maximized;
            MaxImage = "";
        }

        #region Command
        public RelayCommand<object> TabSwitch { get; private set; }
        public RelayCommand WindowCloseCommand { get; private set; }
        public RelayCommand WindowMinCommand { get; private set; }
        public RelayCommand WindowMaxCommand { get; private set; }

        public RelayCommand MenuClick { get; private set; }
        public RelayCommand Menu2Click { get; private set; }
        public RelayCommand ChangePasswordClick { get; private set; }
        public RelayCommand LoginClick { get; private set; }
        public RelayCommand ProjectClick { get; private set; }
        public RelayCommand AuthorityClick { get; private set; }

        public RelayCommand SettingClick { get; private set; }

        public RelayCommand ToMap { get; private set; }

        public MainViewData Data { get; private set; }

        public RelayCommand<Button> TabSwitchWithTwo { get; private set; }
        #endregion

        #region 页面绑定

        //内容呈现器绑定到UserContent
        private UserControl _content;
        public UserControl UserContent
        {
            get => _content;
            set
            {
                _content = value;
                RaisePropertyChanged("UserContent");
            }
        }
        #endregion
    }

    public class MainViewData
    {
        public string ProName { get; set; }

        public string SystemName { get; set; }

        public string UserName { get; set; }

        public string UserImage { get; set; }
    }
}
